import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class Search:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com")
        self.driver.maximize_window()

    # 基础选择器
    def search(self):
        # ID选择器
        print(self.driver.find_element(By.CSS_SELECTOR, "#kw").get_attribute("outerHTML"))
        # class选择器
        print(self.driver.find_element(By.CSS_SELECTOR, ".s_ipt").get_attribute("outerHTML"))
        # 标签+属性
        print(self.driver.find_element(By.CSS_SELECTOR, "input[name='wd']").get_attribute("outerHTML"))

        # 层级
        print(self.driver.find_element(By.CSS_SELECTOR, "form[id='form'] input").get_attribute("outerHTML"))
        #  模糊匹配关键字
        print(self.driver.find_element(By.CSS_SELECTOR, "input[name*='_idx']").get_attribute("outerHTML"))
        # 多条件查找
        print(self.driver.find_element(By.CSS_SELECTOR, "input[name='wd'][autocomplete='off']").get_attribute("outerHTML"))


    def quit(self, seconds=3):
        time.sleep(seconds)
        self.driver.quit()


if __name__ == '__main__':
    IS = Search()
    IS.search()
    IS.quit()
